#coding=utf-8
#import libs 
import sys
import os
from   os.path import abspath, dirname
sys.path.insert(0,abspath(dirname(__file__)))
import console_cmd
import console_sty
import Fun
import EXUIControl
EXUIControl.FunLib = Fun
EXUIControl.G_ExeDir = Fun.G_ExeDir
EXUIControl.G_ResDir = Fun.G_ResDir
import tkinter
from   tkinter import *
import tkinter.ttk
import tkinter.font
from   PIL import Image,ImageTk

#Add your Varial Here: (Keep This Line of comments)
#Define UI Class
class  console:
    def __init__(self,root,isTKroot = True,params=None):
        uiName = Fun.GetUIName(root,self.__class__.__name__)
        self.uiName = uiName
        Fun.Register(uiName,'UIClass',self)
        self.root = root
        self.isTKroot = isTKroot
        self.firstRun = True
        Fun.G_UIParamsDictionary[uiName]=params
        Fun.G_UICommandDictionary[uiName]=console_cmd
        Fun.Register(uiName,'root',root)
        style = console_sty.SetupStyle(isTKroot)
        if isTKroot == True:
            Fun.SetTitleBar(root,titleText='Form1',isDarkMode=False)
            Fun.CenterDlg(uiName,root,1979,753)
            Fun.WindowDraggable(root,False,0,'#ffffff')
            root.wm_attributes("-topmost",1)
            root['background'] = '#EFEFEF'
        Form_1= tkinter.Canvas(root,width = 10,height = 4)
        Form_1.pack(side=tkinter.TOP,fill=tkinter.BOTH,expand=True)
        Form_1.configure(width = 1979)
        Form_1.configure(height = 753)
        Form_1.configure(bg = "#EFEFEF")
        Form_1.configure(highlightthickness = 0)
        Fun.Register(uiName,'Form_1',Form_1)
        Fun.SetUIRootSize(uiName,1979,753)
        #Create the elements of root 
        Frame_2 = tkinter.Frame(Form_1)
        Fun.Register(uiName,'PyMe_Frame_2',Frame_2,'Frame_1')
        Fun.SetControlPlace(uiName,'PyMe_Frame_2',0,-2,486,757,'nw',True,True)
        Frame_2.configure(bg = "#5D9CEC")
        Text_4= EXUIControl.CustomText(Frame_2,True)
        Fun.Register(uiName,'PyMe_Text_4',Text_4,'Text_1')
        Text_4.SetRelief("sunken")
        Fun.SetControlPlace(uiName,'PyMe_Text_4',55,234,375,395,'nw',True,True)
        Button_5 = tkinter.Button(Frame_2,text="发送")
        Fun.Register(uiName,'PyMe_Button_5',Button_5,'Button_1')
        Fun.SetControlPlace(uiName,'PyMe_Button_5',172,661,120,48,'nw',True,True)
        Button_5.configure(bg = "#EFEFEF")
        Button_5.configure(fg = "#000000")
        Button_5.configure(command=lambda:Fun.CommandFunction_Adaptor(console_cmd.Button_1_onCommand,uiName,"Button_1"))
        Label_16 = tkinter.Label(Frame_2,text="发布主题")
        Fun.Register(uiName,'PyMe_Label_16',Label_16,'Label_3')
        Fun.SetControlPlace(uiName,'PyMe_Label_16',182,29,100,48,'nw',True,True)
        Label_16.configure(bg = "#EFEFEF")
        Label_16.configure(fg = "SystemButtonText")
        Label_17 = tkinter.Label(Frame_2,text="发布信息")
        Fun.Register(uiName,'PyMe_Label_17',Label_17,'Label_4')
        Fun.SetControlPlace(uiName,'PyMe_Label_17',182,173,100,48,'nw',True,True)
        Label_17.configure(bg = "#EFEFEF")
        Label_17.configure(fg = "SystemButtonText")
        ComboBox_20_Variable = Fun.AddTKVariable(uiName,'PyMe_ComboBox_20')
        ComboBox_20 = tkinter.ttk.Combobox(Frame_2,textvariable=ComboBox_20_Variable, state="normal",style="ComboBox_1.TCombobox")
        Fun.Register(uiName,'PyMe_ComboBox_20',ComboBox_20,'ComboBox_1')
        Fun.SetControlPlace(uiName,'PyMe_ComboBox_20',51,93,372,48,'nw',True,True)
        ComboBox_20["values"]=['/devices/{deviceid}/login/up','/devices/{deviceid}/logout/up','/devices/{deviceid}/report','/devices/{deviceid}/subscribe','/devices/{deviceid}/devlog/up','/devices/{deviceid}/planning-find','/devices/{deviceid}/planning-refuse','/devices/{deviceid}/planning-arrive','/devices/{deviceid}/command-s2v-resp','/devices/{deviceid}/command-v2s']
        ComboBox_20.current(0)
        ComboBox_20.bind("<<ComboboxSelected>>",Fun.EventFunction_Adaptor(console_cmd.ComboBox_1_onSelect,uiName=uiName,widgetName="ComboBox_1"))
        Frame_7 = tkinter.Frame(Form_1)
        Fun.Register(uiName,'PyMe_Frame_7',Frame_7,'Frame_2')
        Fun.SetControlPlace(uiName,'PyMe_Frame_7',482,-8,1499,760,'nw',True,True)
        Frame_7.configure(bg = "#CCCCCC")
        CheckButton_8_Variable = Fun.AddTKVariable(uiName,'PyMe_CheckButton_8')
        CheckButton_8_Variable.set(False)
        CheckButton_8 = tkinter.Checkbutton(Frame_7,variable=CheckButton_8_Variable,text="全部",anchor=tkinter.W)
        Fun.Register(uiName,'PyMe_CheckButton_8',CheckButton_8,'CheckButton_1')
        Fun.SetControlPlace(uiName,'PyMe_CheckButton_8',758,44,76,40,'nw',True,True)
        CheckButton_8.configure(bg = "#EFEFEF")
        CheckButton_8.configure(fg = "#000000")
        CheckButton_8.configure(activebackground = "#EFEFEF")
        CheckButton_8.configure(activeforeground = "#000000")
        CheckButton_8.configure(selectcolor = "#EFEFEF")
        Fun.SetCheckButtonSelectedColor(uiName,'PyMe_CheckButton_8','#EFEFEF','#000000')
        CheckButton_9_Variable = Fun.AddTKVariable(uiName,'PyMe_CheckButton_9')
        CheckButton_9_Variable.set(False)
        CheckButton_9 = tkinter.Checkbutton(Frame_7,variable=CheckButton_9_Variable,text="发送",anchor=tkinter.W)
        Fun.Register(uiName,'PyMe_CheckButton_9',CheckButton_9,'CheckButton_2')
        Fun.SetControlPlace(uiName,'PyMe_CheckButton_9',903,44,76,40,'nw',True,True)
        CheckButton_9.configure(bg = "#EFEFEF")
        CheckButton_9.configure(fg = "#000000")
        CheckButton_9.configure(activebackground = "#EFEFEF")
        CheckButton_9.configure(activeforeground = "#000000")
        CheckButton_9.configure(selectcolor = "#EFEFEF")
        Fun.SetCheckButtonSelectedColor(uiName,'PyMe_CheckButton_9','#EFEFEF','#000000')
        CheckButton_11_Variable = Fun.AddTKVariable(uiName,'PyMe_CheckButton_11')
        CheckButton_11_Variable.set(False)
        CheckButton_11 = tkinter.Checkbutton(Frame_7,variable=CheckButton_11_Variable,text="接收",anchor=tkinter.W)
        Fun.Register(uiName,'PyMe_CheckButton_11',CheckButton_11,'CheckButton_3')
        Fun.SetControlPlace(uiName,'PyMe_CheckButton_11',1036,44,76,40,'nw',True,True)
        CheckButton_11.configure(bg = "#EFEFEF")
        CheckButton_11.configure(fg = "#000000")
        CheckButton_11.configure(activebackground = "#EFEFEF")
        CheckButton_11.configure(activeforeground = "#000000")
        CheckButton_11.configure(selectcolor = "#EFEFEF")
        Fun.SetCheckButtonSelectedColor(uiName,'PyMe_CheckButton_11','#EFEFEF','#000000')
        Button_18 = tkinter.Button(Frame_7,text="显示日志")
        Fun.Register(uiName,'PyMe_Button_18',Button_18,'Button_2')
        Fun.SetControlPlace(uiName,'PyMe_Button_18',143,40,120,48,'nw',True,True)
        Button_18.configure(bg = "#EFEFEF")
        Button_18.configure(fg = "#000000")
        Button_18.configure(command=lambda:Fun.CommandFunction_Adaptor(console_cmd.Button_2_onCommand,uiName,"Button_2"))
        Label_12 = tkinter.Label(Frame_7,text="过滤")
        Fun.Register(uiName,'PyMe_Label_12',Label_12,'Label_1')
        Fun.SetControlPlace(uiName,'PyMe_Label_12',623,44,76,40,'nw',True,True)
        Label_12.configure(bg = "#EFEFEF")
        Label_12.configure(fg = "SystemButtonText")
        Button_19 = tkinter.Button(Frame_7,text="暂停日志")
        Fun.Register(uiName,'PyMe_Button_19',Button_19,'Button_3')
        Fun.SetControlPlace(uiName,'PyMe_Button_19',340,40,120,48,'nw',True,True)
        Button_19.configure(bg = "#EFEFEF")
        Button_19.configure(fg = "#000000")
        Button_19.configure(command=lambda:Fun.CommandFunction_Adaptor(console_cmd.Button_3_onCommand,uiName,"Button_3"))
        ListBox_13 = tkinter.Listbox(Frame_7)
        Fun.Register(uiName,'PyMe_ListBox_13',ListBox_13,'ListBox_1')
        Fun.SetControlPlace(uiName,'PyMe_ListBox_13',7,96,1484,653,'nw',True,True)
        ListBox_13.configure(bg = "#EFEFEF")
        ListBox_13.configure(fg = "SystemButtonText")
        ListBox_13.configure(exportselection=False)
        Fun.EnableCtrlCCopyContent(uiName,'PyMe_ListBox_13')
        ListBox_13.configure(selectmode = "extended")
        ListBox_13_VScrollbar = tkinter.ttk.Scrollbar(ListBox_13,orient=tkinter.VERTICAL)
        ListBox_13_VScrollbar.place(x = 1464,y = 0,width = 20,height = 653)
        ListBox_13_VScrollbar.config(command = ListBox_13.yview)
        ListBox_13.config(yscrollcommand = ListBox_13_VScrollbar.set)
        Fun.Register(uiName,'PyMe_ListBox_13_VScrollbar',ListBox_13_VScrollbar)
        #Inital all element's Data 
        Fun.InitElementData(uiName)
        #Call Form_1's OnLoad Function
        Fun.RunForm1_CallBack(uiName,"Load",console_cmd.Form_1_onLoad)
        #Add Some Logic Code Here: (Keep This Line of comments)



        #Exit Application: (Keep This Line of comments)
        if self.isTKroot == True and Fun.GetElement(self.uiName,"root"):
            self.root.protocol('WM_DELETE_WINDOW', self.Exit)
            self.root.bind('<Configure>', self.Configure)
            
    def GetRootSize(self):
        return Fun.GetUIRootSize(self.uiName)
    def GetAllElement(self):
        return Fun.G_UIElementDictionary[self.uiName]
    def Escape(self,event):
        if Fun.AskBox('提示','确定退出程序？') == True:
            self.Exit()
    def Exit(self):
        if self.isTKroot == True:
            Fun.DestroyUI(self.uiName)

    def Configure(self,event):
        Form_1 = Fun.GetElement(self.uiName,'Form_1')
        if Form_1 == event.widget:
            Fun.ReDrawCanvasRecord(self.uiName)
        if self.root == event.widget:
            Fun.ResizeRoot(self.uiName,self.root,event)
            Fun.ResizeAllChart(self.uiName)
            uiName = self.uiName
            pass
        Fun.ActiveElement(self.uiName,event.widget)
#Create the root of Kinter 
if  __name__ == '__main__':
    Fun.RunApplication(console)
